package com.fbli.Thread;
//进程Application

/**
 * 实现Runnable接口方式更加适合多个线程共享一个资源的情况，并且避免了单继承的限制
 * 建议使用Runnable方式
 */
public class Thread03 {
    //主线程main
    public static void main(String[] args) {
        T1 t1 = new T1();
        T2 t2 = new T2();
        //子线程t1,t2
        Thread thread1 = new Thread(t1);
        Thread thread2 = new Thread(t2);
        thread1.start();
        thread2.start();
    }
}
class T1 implements Runnable{
    int count = 0;
    @Override
    public void run() {
        while (true){
            System.out.println("hello world" + (++count) +"当前线程："+ Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count == 10){
                break;
            }
        }
    }
}
class T2 implements Runnable{
    int count = 0;
    @Override
    public void run() {
        while (true){
            System.out.println("hi" + (++count)+"当前线程："+ Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count == 10){
                break;
            }
        }
    }
}
